package com.powerbank.devicecenter.controller;

import com.powerbank.common.dto.DeviceDTO;
import com.powerbank.common.dto.DeviceLocationRequest;
import com.powerbank.common.dto.DeviceStatusRequest;
import com.powerbank.common.result.PageResult;
import com.powerbank.common.result.Result;
import com.powerbank.devicecenter.service.DeviceService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.util.List;

/**
 * 设备控制器
 */
@Tag(name = "设备管理", description = "设备相关接口")
@RestController
@RequestMapping("/api/devices")
@RequiredArgsConstructor
public class DeviceController {

    private final DeviceService deviceService;

    @Operation(summary = "根据ID查询设备", description = "根据设备ID查询设备详情")
    @GetMapping("/{deviceId}")
    public Result<DeviceDTO> getDeviceById(@Parameter(description = "设备ID") @PathVariable Long deviceId) {
        return deviceService.getDeviceById(deviceId);
    }

    @Operation(summary = "根据编号查询设备", description = "根据设备编号查询设备详情")
    @GetMapping("/code/{deviceCode}")
    public Result<DeviceDTO> getDeviceByCode(@Parameter(description = "设备编号") @PathVariable String deviceCode) {
        return deviceService.getDeviceByCode(deviceCode);
    }

    @Operation(summary = "查询可用设备", description = "查询所有可用设备列表")
    @GetMapping("/available")
    public Result<List<DeviceDTO>> getAvailableDevices() {
        return deviceService.getAvailableDevices();
    }

    @Operation(summary = "查询附近设备", description = "根据位置查询附近设备")
    @GetMapping("/nearby")
    public Result<List<DeviceDTO>> getNearbyDevices(
            @Parameter(description = "经度") @RequestParam BigDecimal longitude,
            @Parameter(description = "纬度") @RequestParam BigDecimal latitude,
            @Parameter(description = "搜索半径（米）") @RequestParam(defaultValue = "1000") Integer radius) {
        return deviceService.getNearbyDevices(longitude, latitude, radius);
    }

    @Operation(summary = "根据站点查询设备", description = "分页查询指定站点的设备列表")
    @GetMapping("/station/{stationId}")
    public Result<PageResult<DeviceDTO>> getDevicesByStation(
            @Parameter(description = "站点ID") @PathVariable Long stationId,
            @Parameter(description = "页码") @RequestParam(defaultValue = "1") Integer page,
            @Parameter(description = "页大小") @RequestParam(defaultValue = "10") Integer size) {
        return deviceService.getDevicesByStation(stationId, page, size);
    }

    @Operation(summary = "锁定设备", description = "为订单锁定设备")
    @PostMapping("/{deviceId}/lock")
    public Result<Void> lockDevice(
            @Parameter(description = "设备ID") @PathVariable Long deviceId,
            @Parameter(description = "订单号") @RequestParam String orderNo) {
        return deviceService.lockDevice(deviceId, orderNo);
    }

    @Operation(summary = "解锁设备", description = "为订单解锁设备")
    @PostMapping("/{deviceId}/unlock")
    public Result<Void> unlockDevice(
            @Parameter(description = "设备ID") @PathVariable Long deviceId,
            @Parameter(description = "订单号") @RequestParam String orderNo) {
        return deviceService.unlockDevice(deviceId, orderNo);
    }

    @Operation(summary = "启动设备", description = "启动设备开始使用")
    @PostMapping("/{deviceId}/start")
    public Result<Void> startDevice(
            @Parameter(description = "设备ID") @PathVariable Long deviceId,
            @Parameter(description = "订单号") @RequestParam String orderNo) {
        return deviceService.startDevice(deviceId, orderNo);
    }

    @Operation(summary = "停止设备", description = "停止设备使用")
    @PostMapping("/{deviceId}/stop")
    public Result<Void> stopDevice(
            @Parameter(description = "设备ID") @PathVariable Long deviceId,
            @Parameter(description = "订单号") @RequestParam String orderNo) {
        return deviceService.stopDevice(deviceId, orderNo);
    }

    @Operation(summary = "更新设备状态", description = "更新设备状态信息")
    @PutMapping("/status")
    public Result<Void> updateDeviceStatus(@Validated @RequestBody DeviceStatusRequest request) {
        return deviceService.updateDeviceStatus(request);
    }

    @Operation(summary = "更新设备位置", description = "更新设备位置信息")
    @PutMapping("/location")
    public Result<Void> updateDeviceLocation(@Validated @RequestBody DeviceLocationRequest request) {
        return deviceService.updateDeviceLocation(request);
    }

    @Operation(summary = "设备心跳", description = "处理设备心跳信号")
    @PostMapping("/heartbeat/{deviceCode}")
    public Result<Void> handleDeviceHeartbeat(@Parameter(description = "设备编号") @PathVariable String deviceCode) {
        return deviceService.handleDeviceHeartbeat(deviceCode);
    }

    @Operation(summary = "查询离线设备", description = "查询离线设备列表")
    @GetMapping("/offline")
    public Result<List<DeviceDTO>> getOfflineDevices() {
        return deviceService.getOfflineDevices();
    }

    @Operation(summary = "查询低电量设备", description = "查询低电量设备列表")
    @GetMapping("/low-battery")
    public Result<List<DeviceDTO>> getLowBatteryDevices() {
        return deviceService.getLowBatteryDevices();
    }

    @Operation(summary = "设备故障报告", description = "报告设备故障")
    @PostMapping("/{deviceId}/fault")
    public Result<Void> reportDeviceFault(
            @Parameter(description = "设备ID") @PathVariable Long deviceId,
            @Parameter(description = "故障描述") @RequestParam String faultDescription) {
        return deviceService.reportDeviceFault(deviceId, faultDescription);
    }

    @Operation(summary = "设备维护完成", description = "标记设备维护完成")
    @PostMapping("/{deviceId}/maintenance/complete")
    public Result<Void> completeDeviceMaintenance(@Parameter(description = "设备ID") @PathVariable Long deviceId) {
        return deviceService.completeDeviceMaintenance(deviceId);
    }

    @Operation(summary = "批量更新设备状态", description = "批量更新设备状态")
    @PutMapping("/batch/status")
    public Result<Void> batchUpdateDeviceStatus(
            @Parameter(description = "设备ID列表") @RequestParam List<Long> deviceIds,
            @Parameter(description = "状态") @RequestParam String status) {
        return deviceService.batchUpdateDeviceStatus(deviceIds, status);
    }

    @Operation(summary = "设备统计信息", description = "获取设备统计信息")
    @GetMapping("/statistics")
    public Result<Object> getDeviceStatistics() {
        return deviceService.getDeviceStatistics();
    }
}